package research.ui.editors;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.forms.widgets.TableWrapData;

import research.domain.Sample;
import research.domain.Viscosity;
import research.domain.ViscosityValue;
import research.entity.Entity;
import research.entity.EntityType;
import research.model.EntityListContentProvider;
import research.model.ListHierarchy;
import research.model.ViscosityHierarchy;
import research.persistence.PersistenceManager;
import research.ui.editors.input.EntityEditorInput;
import research.ui.editors.tableeditors.viscosity.ViscosityTableEditor;

public class ViscosityEditor extends FormEntityEditor {
	public static String ID = "research.ui.editors.ViscosityEditor";
	private ComboViewer sampleCombo;
	private Text name;
	private Text idx;
	private ViscosityTableEditor valuesEditor;
	private List<Entity> values;
	private List<Entity> initValues;
	
	
	private Viscosity getEntity() {
		return (Viscosity) entity;
	}

	@Override
	protected boolean checkInputEntityType(EntityEditorInput input) {
		return input.getEntity().getType().equals(EntityType.Viscosity);
	}

	@Override
	protected void fill() {
		this.name.setText(this.getEntity().getName());
		this.idx.setText(this.getEntity().getIdx());

		setComboEntity(sampleCombo, this.getEntity().getSample());
		
		this.valuesEditor.setInput(new ArrayList<Entity>(values));
		valuesEditor.addModifyListener(getEntityTableListener());
		
	}
	
	@Override
	protected void initialize() {
		if (!isNewEntity()) {
			values = PersistenceManager.getInstance().getDataSource().getChildren(getEntity(), new ViscosityHierarchy());
		} else {
			values = new ArrayList<Entity>();
		}
		
		initValues = new ArrayList<Entity>(values);
	}

	@Override
	protected void persist() {
		for (Entity oldpar : initValues) {
			if (!valuesEditor.getEntityList().getEntities().contains(oldpar)) {
				getPersistenceManager().remove(oldpar);
			}
		}

		for (Entity par : valuesEditor.getEntityList().getEntities()) {
			ViscosityValue val = (ViscosityValue) par;
			val.setViscosity(getEntity());
			getPersistenceManager().persist(val);
		}
	}

	@Override
	protected boolean readAndValidate() {
		this.getEntity().setName(this.name.getText());
		this.getEntity().setIdx(this.idx.getText());
		Sample cal = (Sample) getComboEntity(sampleCombo);

		if (cal == null) {
			MessageDialog.openWarning(getSite().getShell(), "Ошибка",
					"Выберите образец");
			return false;
		}

		this.getEntity().setSample(cal);
		
		return true;
	}

	@Override
	protected void renderForm() {
		TableWrapData td;
		Label label;
		Section section;
		Composite composite;
		GridData gd;
		
		/*
		 * Research
		 */
		section = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
		td = new TableWrapData(TableWrapData.FILL);
		section.setLayoutData(td);
		section.setText("Образец пленки");
		// section.setDescription("Select calander from the list");
		composite = toolkit.createComposite(section);
		composite.setLayout(new GridLayout(2, false));
		section.setClient(composite);
		gd = new GridData();
		label = toolkit.createLabel(composite, "Выберите образец*:");
		label.setLayoutData(gd);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		sampleCombo = new ComboViewer(composite, SWT.DROP_DOWN
				| SWT.READ_ONLY);
		sampleCombo.setContentProvider(new EntityListContentProvider(new ListHierarchy(EntityType.Sample), false));
		sampleCombo.setLabelProvider(new LabelProvider());
		sampleCombo.setInput(PersistenceManager.getInstance());
		gd.widthHint = 100;
		sampleCombo.getCombo().setLayoutData(gd);
		sampleCombo.getCombo().addModifyListener(getTextModifyListener());

		/*
		 * Name
		 */
		section = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
		td = new TableWrapData(TableWrapData.FILL_GRAB);
		section.setLayoutData(td);
		section.setText("Название");
		composite = toolkit.createComposite(section);
		composite.setLayout(new GridLayout(1, false));
		section.setClient(composite);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		name = toolkit.createText(composite, "",
				SWT.BORDER);
		gd.widthHint = 100;
		name.setLayoutData(gd);
		name.addModifyListener(getNameModifyListener());
		
		/*
		 * Idx
		 */
		section = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
		td = new TableWrapData(TableWrapData.FILL_GRAB);
		section.setLayoutData(td);
		section.setText("Индекс");
		composite = toolkit.createComposite(section);
		composite.setLayout(new GridLayout(1, false));
		section.setClient(composite);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		idx = toolkit.createText(composite, "",
				SWT.BORDER);
		gd.widthHint = 100;
		idx.setLayoutData(gd);
		idx.addModifyListener(getTextModifyListener());
		
		/*
		 * Values
		 */
		section = toolkit.createSection(form.getBody(), Section.TITLE_BAR
				| Section.TWISTIE | Section.EXPANDED);
		td = new TableWrapData(TableWrapData.FILL);
		td.maxHeight = 200;
		section.setLayoutData(td);
		section.setText("Результаты измерений");
		composite = toolkit.createComposite(section);
		section.setClient(composite);
		valuesEditor = new ViscosityTableEditor(composite);
		valuesEditor.setInput(new ArrayList<Entity>());
		valuesEditor.addModifyListener(getEntityTableListener());
		
	}

}
